package com.example.italkapp2.controller.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.example.italkapp2.R;
import com.example.italkapp2.model.Model;
import com.example.italkapp2.model.bean.UserInfo;
import com.hyphenate.chat.EMClient;


public class SplashActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_slash);

        handler.sendMessageDelayed(Message.obtain(),2000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacksAndMessages(null);
    }

    @SuppressLint("HandlerLeak")
    private Handler handler=new Handler(){
        public void handleMessage(Message msg){
            //如果activity已退出，就结束handler的处理
            if(isFinishing()){
                return;
            }

            //判断进入主页面还是登陆页面（是否已经记住登录）
            toMainOrLogin();
        }
    };

    private void toMainOrLogin(){
        /*new Thread(){
            public void run(){

            }
        }.start();*/

        //改为如下使用全局线程池
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //判断是否已经登陆过
                if(EMClient.getInstance().isLoggedInBefore()){

                    //获取到当前登录用户的信息
                    UserInfo account=Model.getInstance().getUserAccountDao().getAccountByHxid(EMClient.getInstance().getCurrentUser());
                    if(account!=null){
                        //跳转到主页面
                        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                        startActivity(intent);

                        //登陆成功后的方法
                        Model.getInstance().loginSuccess(account);
                    }else{

                        //跳转到登陆界面
                        Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                        startActivity(intent);
                    }
                }else{
                    //跳转到登陆界面
                    Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                //结束当前页面
                finish();
            }
        });
    }
}